package fulltxt;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.attilax.archive.pdfutilV2;
import com.attilax.core.ForeachFunction;
import com.attilax.io.DirTraveService;
import com.attilax.io.FileUtilsAti;
import com.attilax.web.es.esUtil_docx2;

import data.html.html2txtConverter;
import officefile.ExcelToTxt;
import officefile.wordutil;

public class docuReader {
	static final Logger logger = Logger.getLogger(docuReader.class);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		
		String dir="C:\\atibeks517\\l4 other doc v4 s56 oriVer exted\\xls";
		String dir_tar="C:\\atibeks517\\l4 other doc v4 s56 oriVer exted 2000-201512 txtVer\\xls";
		new docuReader().read2TxtTravedir_geneTxtVer(dir,dir_tar);

	}
	
	public void read2TxtTravedir_geneTxtVer(String dir,String dir_tar) {
		new DirTraveService().traveV5_vS522(new File(dir), new ForeachFunction() {

			@Override
			public void each(int count, final File f) throws Exception {
			
				if(f.length()>5*1000000)//5M
					return;
				String lowerCase = f.getAbsolutePath().toLowerCase();
				
				if( FileUtilsAti.isNotDocFmt(lowerCase))
					return;
				
				try {		
					logger.info("cn:" + count + f.getAbsolutePath());
				//	final String t = FileUtilsAti.readFileToStringAutoDetectEncode(f.getAbsolutePath());
					
					String txt = read2Txt_byFile( f.getAbsolutePath());
					
					logger.info(  f.getName() + txt);
				   String rltpath=FileUtilsAti.rltpath(f.getAbsolutePath(), dir);
				 	String newpath = dir_tar+File.separator+ rltpath+".txt";
					FileUtils.write(new File(newpath), txt,false);
					
				} catch (Exception e) {
					logger.error(e);
				}
				
				 

			}

			

		 

		});
	}

	
	public void read2TxtTravedir(String dir,String dir_tar) {
		new DirTraveService().traveV5_vS522(new File(dir), new ForeachFunction() {

			@Override
			public void each(int count, final File f) throws Exception {
			
				if(f.length()>5*1000000)//5M
					return;
				String lowerCase = f.getAbsolutePath().toLowerCase();
				
				if( FileUtilsAti.isNotDocFmt(lowerCase))
					return;
				
				try {		
					logger.info("cn:" + count + f.getAbsolutePath());
				//	final String t = FileUtilsAti.readFileToStringAutoDetectEncode(f.getAbsolutePath());
					
					String txt = read2Txt_byFile( f.getAbsolutePath());
					
					logger.info(  f.getName() + txt);
				
				 //	FileUtils.write(new File(dir_tar+"\\s1-s523 log.txt"), txt,true);
					
				} catch (Exception e) {
					logger.error(e);
				}
				
				 

			}

			

		 

		});
	}

	public String read2Txt_byFile(String path2) {
		String path = path2.toLowerCase();
		if(FileUtilsAti.isExcelfile(path))
			return  ExcelToTxt.exexl2txtAllfmt(path);
		if (path.endsWith("docx")) {
			
			return new wordutil().readWord(path);
      
		} else if (path.endsWith("doc")) {
			return new wordutil().readWord(path);
		}else if(path.endsWith("html") || path.endsWith("htm"))
		{
			return	html2txtConverter.html2txtV2(path);
		} else if (path.endsWith(".pdf")) {
			return   pdfutilV2.convert2txt_consoleBlockmode(path);
		}
		else if (path.endsWith(".ppt")) {
			return  wordutil.readPPT(path);
		}
		
		
		
		return FileUtilsAti.readFileToStringAutoDetectEncode(path2);
	}
}
